package com.nartosoft.tubuloader;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;

public class TubuloaderTray extends EventListenerAdapter {

	TrayIcon ti = null;

	int done = 1;

	int totalNumber = 0;

	String fileName = null;
	
	boolean errors = false;

	public TubuloaderTray(String fileName, int totalNumber) {
			this.fileName = fileName;
			this.totalNumber = totalNumber;
			this.setIcon();
	}

	public void onAllFilesDone() {
		SystemTray tray = SystemTray.getSystemTray();
		tray.remove(ti);
	}

	public void onUpload(String fileName, String url) {
		this.done++;
		this.updateIcon();
	}

	public void onError(String message) {
		this.done++;
		this.errors = true;
		this.updateIcon();
	}
	
	public void setIcon() {
		try {
			SystemTray tray = SystemTray.getSystemTray();
			URL url = new URL("file:"
					+ new File(this.fileName).getCanonicalFile());
			ti = new TrayIcon(Toolkit.getDefaultToolkit().getImage(url));
			String tooltip = "Uploading file " + done + "/" + totalNumber;
			if(this.errors) {
		      tooltip+=" (with Errors)";
			}
			PopupMenu menu = new PopupMenu("Tubuloader");
			MenuItem menuItem1 = new MenuItem("Abort");
			menuItem1.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent evt) {
			    System.exit(0);
			  }}
			);
			menu.add(menuItem1);
			ti.setPopupMenu(menu);
			ti.setToolTip(tooltip);
			tray.add(ti);
		} catch (Exception e) {
			// e.printStackTrace();
		}
	}
	
	public void updateIcon() {
	}

	public static void main(String[] args) {
		 //new PicUploaderTray("upload.gif");
		//new PicUploaderTray(10);
	}

}
